package com.wl.api.controller.system;

import java.util.function.Predicate;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wl.api.common.annotation.RequiresAuthentication;
import com.wl.api.common.utils.CollectionUtil;
import com.wl.api.common.utils.ErrorCodeEnum;
import com.wl.api.common.utils.JsonObject;
import com.wl.api.common.utils.StringUtil;
import com.wl.api.common.utils.constant.AppEnum;
import com.wl.api.controller.base.CommonController;
import com.wl.api.dto.base.CommonUserDTO;
import com.wl.api.dto.system.SecurityRoleDTO;
import com.wl.api.service.system.ISecurityRoleService;

@Controller
@RequestMapping("/manage/system/role")
@RequiresAuthentication(getClientType=AppEnum.admin_client, getAuthticationSn="role")
public class RoleController extends CommonController {

	@Autowired
	ISecurityRoleService roleService;
	

	@RequestMapping(value="/create", method=RequestMethod.POST)
	@ResponseBody
	public JsonObject createRole(@RequestBody SecurityRoleDTO role)  throws Exception {
		JsonObject json = JsonObject.getInstance();
		boolean pass = true;
		String title = "" , error_code = ""; 
		if (validate().negate().test(role)) {
			title = ErrorCodeEnum.param_error.getMessage();
			error_code = ErrorCodeEnum.param_error.getError_code();
			pass = false;
		}
		if (pass) {
			initRoleData(role);
			json.setData(roleService.createRoleAndPermission(SecurityRoleDTO.builder().name(role.getName()).moduleList(role.getModuleList()).build()));
		}
		setJsonMessage(json, title, error_code, pass);
		return json;
	}
	
	
	
	@RequestMapping(value = "/create{id}", method = RequestMethod.PUT)
	@ResponseBody
	public JsonObject modify(@PathVariable("id") long id, @RequestBody SecurityRoleDTO role) throws Exception {
		JsonObject json = JsonObject.getInstance();
		boolean pass = true;
		String title = "" , error_code = ""; 
		if (validate().and(s -> s.getId() != null).negate().test(role)) {
			title = ErrorCodeEnum.param_error.getMessage();
			error_code = ErrorCodeEnum.param_error.getError_code();
			pass = false;
		}
		if (pass) {
			initRoleData(role);
			json.setData(roleService.updateRoleAndPermission(role));
		}
		setJsonMessage(json, title, error_code, pass);
		return json;
	}
	
	@RequestMapping(value = "/create/{id}", method = RequestMethod.DELETE)
	@ResponseBody
	public JsonObject deleted(@PathVariable("id") Integer id) throws Exception {
		JsonObject json = JsonObject.getInstance();
		SecurityRoleDTO role = SecurityRoleDTO.builder().id(new Long(id)).isDeleted(true).build();
		initRoleData(role);
		roleService.updateRoleAndPermission(role);
		json.setData(id);
	 	setJsonMessage(json);
		return json;
	}
	
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	@ResponseBody
	public JsonObject getRoleList(String name) {
		JsonObject json = JsonObject.getInstance();
		json.setData(roleService.getRoleListPage(SecurityRoleDTO.builder().name(name).build()));
		setJsonMessage(json);
		return json;
	}
	
	@RequestMapping(value = "/createUser/{id}", method = RequestMethod.GET)
	@ResponseBody
	public JsonObject getRoleDetail(@PathVariable("id")Integer id) {
		JsonObject json = JsonObject.getInstance();
		json.setData(roleService.getRolePermissionDetail(new Long(id)));
		setJsonMessage(json);
		return json;
	}
	

	private void initRoleData(SecurityRoleDTO role) throws Exception {
		CommonUserDTO userDTO = getCommonUser();
		role.buildMsg(userDTO.getId(), userDTO.getUsername());
	}
	
	private Predicate<SecurityRoleDTO> validate() {
		return s -> StringUtil.isNotEmpty(s.getName()) && !CollectionUtil.isEmpty(s.getModuleList());
	}
}
